package com.czd.support.controller;

import com.czd.base.model.JsonResult;
import com.czd.base.utils.WebUtils;
import com.czd.support.enums.CodeTypeEnum;
import com.czd.support.model.Manager;
import com.czd.support.service.ManagerService;
import com.czd.support.util.MD5Utils;
import com.czd.support.util.VerifyCodeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * Created with IntelliJ IDEA.
 * Author: Jerry.hu
 * Create: Jerry.hu (14-9-25 18:53)
 * Description:
 * To change this template use File | Settings | File Templates.
 */
@Controller
@RequestMapping("/managers")
public class ManagerController extends BaseController{

	@Autowired
	private ManagerService managerService;
	/**
	 * 管理页列表页信息
	 * @return
	 */
	@RequestMapping(value = {"","/list"},method = RequestMethod.GET)
	public String manager(Model model){
		model.addAttribute("managerList",managerService.queryForList());
		return "manager/list";
	}

	/**
	 *
	 * @return
	 */
	@RequestMapping(value = {"/add"},method = RequestMethod.GET)
	public String add(){
		return "manager/add";
	}

	@RequestMapping(value = {"/add"},method = RequestMethod.POST)
	public 	@ResponseBody
	JsonResult save(HttpServletRequest request){
		Manager manager = new Manager();
		manager.setCreateUser(this.getSesionUser(request).getUserCode());
		manager.setUserName(WebUtils.findParamStr(request, "userName"));
		String password = VerifyCodeUtils.runVerifyCode(6, CodeTypeEnum.数字字母);
		manager.setPassword(MD5Utils.str2Md5(password));
		if(managerService.insert(manager)){
			return JsonResult.OK.put("password",password).put("userCode",manager.getUserCode());
		}
		return JsonResult.OK.put("password",password);
	}

	@RequestMapping(value = {"/details/{id}"},method = RequestMethod.GET)
	public String view(@PathVariable int id,Model model,HttpServletRequest request){
		Manager manager = managerService.findById(id);
		model.addAttribute("password",WebUtils.findParamStr(request,"password"));
		model.addAttribute("manager",manager);
		return "manager/details";
	}

}
